//
//  ScatterChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/5.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "ScatterChartViewController.h"
@interface ScatterChartViewController ()
@end

@implementation ScatterChartViewController
ScatterChartView *sampleScatterChartView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initChartView{
    sampleScatterChartView=[[ScatterChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleScatterChartView.backgroundColor = [UIColor whiteColor];
    sampleScatterChartView.delegate = self;
    [self.view addSubview:sampleScatterChartView];
    [self initChartStyleAndData];
    [sampleScatterChartView animateWithXAxisDuration:1.4 yAxisDuration:1.4 easingOption:ChartEasingOptionEaseOutBack];
}

-(void)initChartStyleAndData{
    [self setupScatterChartView:sampleScatterChartView];
    [self setScatterChartViewParam:sampleScatterChartView];
    [self setLegendStyle:sampleScatterChartView];
    [self initChartData];
}

- (void)setupScatterChartView:(ScatterChartView *)chartView
{
    chartView.descriptionText = @"descriptionText";
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    
    chartView.drawGridBackgroundEnabled = NO;
    chartView.dragEnabled = YES;
    [chartView setScaleEnabled:YES];
    chartView.maxVisibleValueCount = 200;
    chartView.pinchZoomEnabled = YES;
}

-(void)setScatterChartViewParam:(ScatterChartView *)chartView{
    ChartYAxis *yl = chartView.leftAxis;
    yl.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    yl.axisMinValue = 0.0; // this replaces startAtZero = YES
    
    chartView.rightAxis.enabled = NO;
    
    ChartXAxis *xl = chartView.xAxis;
    xl.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    xl.drawGridLinesEnabled = NO;
}

//设置图例样式
-(void)setLegendStyle:(ScatterChartView *)chartView{
    ChartLegend *l = chartView.legend;
    l.position = ChartLegendPositionRightOfChart;
    l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
}

- (void)initChartData
{
    [self setDataCount:45 range:100 chart:sampleScatterChartView];
}

- (void)setDataCount:(int)count range:(double)range chart:(ScatterChartView *)chartView
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        [xVals addObject:[@(i) stringValue]];
    }
    
    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals3 = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        double val = (double) (arc4random_uniform(range)) + 3;
        [yVals1 addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
        
        val = (double) (arc4random_uniform(range)) + 3;
        [yVals2 addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
        
        val = (double) (arc4random_uniform(range)) + 3;
        [yVals3 addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    
    ScatterChartDataSet *set1 = [[ScatterChartDataSet alloc] initWithYVals:yVals1 label:@"DS 1"];
    set1.scatterShape = ScatterShapeSquare;
    [set1 setColor:ChartColorTemplates.colorful[0]];
    ScatterChartDataSet *set2 = [[ScatterChartDataSet alloc] initWithYVals:yVals2 label:@"DS 2"];
    set2.scatterShape = ScatterShapeCircle;
    set2.scatterShapeHoleColor = ChartColorTemplates.colorful[3];
    set2.scatterShapeHoleRadius = 3.5f;
    [set2 setColor:ChartColorTemplates.colorful[1]];
    ScatterChartDataSet *set3 = [[ScatterChartDataSet alloc] initWithYVals:yVals3 label:@"DS 3"];
    set3.scatterShape = ScatterShapeCross;
    [set3 setColor:ChartColorTemplates.colorful[2]];
    
    set1.scatterShapeSize = 8.0;
    set2.scatterShapeSize = 8.0;
    set3.scatterShapeSize = 8.0;
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];
    [dataSets addObject:set2];
    [dataSets addObject:set3];
    
    ScatterChartData *data = [[ScatterChartData alloc] initWithXVals:xVals dataSets:dataSets];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7.f]];
    
    chartView.data = data;
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end